package com.graduation.project.controller.system;

import com.graduation.project.entity.dto.user.ListUserDTO;
import com.graduation.project.entity.dto.user.SaveOrUpdateUserDTO;
import com.graduation.project.entity.dto.UpdateStatusDTO;
import com.graduation.project.entity.dto.UpdateUserInfoByMySelfDTO;
import com.graduation.project.service.system.UserManagementService;
import com.graduation.project.utils.ResultUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author 卑微小峰
 * @date 2022/11/05
 * 用户管理控制类
 */
@RestController
@RequestMapping("/user_management")
public class UserManagementController {

    @Autowired
    private UserManagementService userManagementService;

    /**
     * 添加用户
     * @param input 新增用户输入类
     * @return ResultUtil
     */
    @PostMapping("/post/addUser")
    public ResultUtil<?> addUser(@RequestBody SaveOrUpdateUserDTO input) {
        return userManagementService.addUser(input);
    }

    /**
     * 修改用户
     * @param input 修改用户输入类
     * @return ResultUtil
     */
    @PutMapping("/put/updateUserById")
    public ResultUtil<?> updateUser(@RequestBody SaveOrUpdateUserDTO input) {
        return userManagementService.updateUser(input);
    }

    /**
     * 删除用户
     * @param idList 选中数据项id集合
     * @return ResultUtil
     */
    @DeleteMapping("/delete/deleteUserById")
    public ResultUtil<?> deleteUserById(@RequestBody List<Long> idList) {
        return userManagementService.deleteUserById(idList);
    }

    /**
     * 根据id修改状态
     * @param input 修改状态输入类
     * @return ResultUtil
     */
    @PutMapping("/put/updateUserStatusById")
    public ResultUtil<?> updateUserStatusById(@RequestBody UpdateStatusDTO input) {
        return userManagementService.updateUserStatusById(input);
    }

    /**
     * （条件）分页查询用户列表
     * @param input 查询用户列表输入类
     * @return ResultUtil
     */
    @ApiOperation("（条件）分页查询用户列表")
    @PostMapping("/post/listUserByCondition")
    public ResultUtil<?> listUserByCondition(@RequestBody ListUserDTO input) {
        return userManagementService.listRoleByCondition(input);
    }

    /**
     * 查询当前用户拥有角色
     * @param userId 用户id
     * @return ResultUtil
     */
    @GetMapping("/get/listUserRoleByUserId/{userId}")
    public ResultUtil<?> listUserRoleByUserId(@PathVariable("userId") Long userId) {
        return userManagementService.listUserRoleByUserId(userId);
    }

    /**
     * 查询角色下拉项数据
     * @return ResultUtil
     */
    @GetMapping("/get/listRoleOptions")
    public ResultUtil<?> listRoleOptions() {
        return userManagementService.listRoleOptions();
    }

    /**
     * 查询单位下拉项数据
     * @return ResultUtil
     */
    @GetMapping("/get/listUnitOptions")
    public ResultUtil<?> listUnitOptions() {
        return userManagementService.listUnitOptions();
    }

    /**
     * 重置密码
     * @param id 用户id
     * @param password 用户密码
     * @return ResultUtil
     */
    @PutMapping("/put/resetUserPassword/{id}/{password}")
    public ResultUtil<?> resetUserPassword(@PathVariable("id") Long id, @PathVariable("password") String password) {
        return userManagementService.resetUserPassword(id, password);
    }

    /**
     * 用户详情（用户自身）
     */
    @GetMapping("/get/getUserInfoDetails/{userCode}")
    public ResultUtil<?> getUserInfoDetails(@PathVariable("userCode") String userCode) {
        return userManagementService.getUserInfoDetails(userCode);
    }

    /**
     * 下载模版
     */
    @GetMapping("/get/downloadFile")
    public ResultUtil<?> download() {
        return userManagementService.download();
    }

    /**
     * 用户修改个人信息
     */
    @PutMapping("/put/updateUserInfoByMySelf")
    public ResultUtil<?> updateUserInfoByMySelf(@RequestBody UpdateUserInfoByMySelfDTO updateUserInfoByMySelfDTO) {
        return userManagementService.updateUserInfoByMySelf(updateUserInfoByMySelfDTO);
    }

    /**
     * 上传头像
     */
    @PostMapping("/post/uploadAvatar")
    public ResultUtil<?> uploadAvatar(@RequestParam MultipartFile file) {
        return userManagementService.uploadAvatar(file);
    }

    /**
     * 导入用户
     */
    @PostMapping("/post/importData")
    public ResultUtil<?> importData(@RequestParam MultipartFile file) {
        System.out.println(file);
        return null;
    }

    /**
     * 导出用户
     */

    /**
     * 给用户分配角色
     * @param id 用户id
     * @param idList 角色id集合
     * @return ResultUtil
     */
    @PutMapping("/update/assignRoleToUser/{id}")
    public ResultUtil<?> assignRoleToUser(@PathVariable("id") Long id, @RequestBody List<Long> idList) {
        return userManagementService.assignRoleToUser(id, idList);
    }
}
